package com.demo.service;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.config.Task;

public class BaseService {
	// 部署流程实例相关
	@Autowired
	private RepositoryService reService;
	// 创建执行流程实例相关
	@Autowired
	ExecutionService executionService;
	// 提供对任务列表的访问接口
	@Autowired
	TaskService taskService;

	// 流程定义的部署
	/**
	 * 
	 * @param filepath xml文件
	 */
	public void respositoryService(String filepath) {
		this.reService.createDeployment().addResourceFromClasspath(filepath).deploy();
	}

	/**
	 * 删除流程定义（如果带有流程实例没有完成的话会抛出异常，而deleteDeploymentCasecade不会，他会将你的没有完成的流程实例一并删除）
	 */
	public void deleteDeployment(String deploymentId) {
		this.reService.deleteDeployment(deploymentId);
		System.out.println("删除成功");
	}

	/**
	 * 删除流程定义与历史信息
	 */
	public void deleteCacheDeployment(String deploymentId) {
		this.reService.deleteDeploymentCascade(deploymentId);
		System.out.println("删除成功");
	}

	/**
	 * 
	 * @param processDefinitionKey 流程实例的key
	 */
	// 创建流程实例
	public void initInstanceService(String processDefinitionKey) {
		ProcessInstance processInstance = this.executionService.startProcessInstanceByKey(processDefinitionKey);
		System.out.println("流程实例ID=======》" + processInstance.getId());
	}

	public void initInstanceByparameService(String processDefinitionKey, Map<String, Object> map) {
		ProcessInstance processInstance = this.executionService.startProcessInstanceByKey(processDefinitionKey, map);
		System.out.println("流程实例ID------》" + processInstance.getId());
	}

	/**
	 * 删除流程实例
	 * 
	 * @param processInstanceId
	 */
	public void deleteInstanceById(String processInstanceId) {
		this.executionService.deleteProcessInstance(processInstanceId);
		System.out.println("删除成功");
	}

	// 执行流程
	/**
	 * 
	 * @param taskId 活动流程id
	 */
	public void completeTaskService(String taskId) {
		this.taskService.completeTask(taskId);
		System.err.println("完成成功");
	}

	// executionService.createProcessInstanceQuery().processInstanceId("test.10001").uniqueResult().findActiveActivityNames().toString();
	// 根据流程实例Id查询流程所在节点
	/**
	 * 
	 * @param processDefinitionId 流程实例Id
	 * @return 流程实例所在节点的name
	 */
	public String getCurrectActivity(String processDefinitionId) {
		String activityName = this.executionService.createProcessInstanceQuery().processDefinitionId(processDefinitionId).uniqueResult()
				.findActiveActivityNames().toString();
		return activityName;
	}

	/**
	 * 
	 * @param userName 用户名
	 * @return 该用户的活动信息 1 任务数量 2 任务id 3 任务名称 4 任务人员 。。。。
	 */
	public List<Task> getTaskByName(String userName) {
		List<Task> tasks = this.taskService.findPersonalTasks(userName);
		System.out.println("任务数量==" + tasks.size());
		Task task = tasks.get(0);

		System.out.println("任务名词==" + task.getActivityName());
		System.out.println("任务人员==" + task.getAssignee() + "任务ID===" + task.getId());
		return null;
	}
}
